﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace MuaBanLike.com
{
    public partial class MainWindow : Window
    {

        private void assignEventPopup()
        {
            Guid SID_SWebBrowserApp = new Guid("0002DF05-0000-0000-C000-000000000046");

            try
            {
                //this is very important because we need a reference on Document not null!
                IServiceProvider serviceProvider = (IServiceProvider)MainBrowser.Document;
                Guid serviceGuid = SID_SWebBrowserApp;
                Guid iid = typeof(SHDocVw.IWebBrowser2).GUID;
                SHDocVw.IWebBrowser2 myWebBrowser2 = (SHDocVw.IWebBrowser2)serviceProvider.QueryService(ref serviceGuid, ref iid);


                SHDocVw.DWebBrowserEvents_Event wbEvents = (SHDocVw.DWebBrowserEvents_Event)myWebBrowser2;

                SHDocVw.DWebBrowserEvents2_Event wbEvents2 = (SHDocVw.DWebBrowserEvents2_Event)myWebBrowser2;

                //this is the real event handling done
                // wbEvents.NewWindow += new SHDocVw.DWebBrowserEvents_NewWindowEventHandler(OnWebBrowserNewWindow);
                wbEvents2.NewWindow2 += new SHDocVw.DWebBrowserEvents2_NewWindow2EventHandler(wbEvents2_NewWindow2);

            }
            catch (Exception exc)
            {

            }
        }

        void wbEvents2_NewWindow2(ref object ppDisp, ref bool Cancel)
        {           
            //startTime = DateTime.Now;
            if (wnd != null)
            {
                wnd.disposeWebControl();
                wnd.Close();
                wnd = null;
            }

            //startCountTimeThread();
            wnd = new PopupWindow();
            wnd.mainWnd = this;
            wnd.slink = m_likeItem.LikelLink;
            //wnd.Show();
            //Just navigate to make sure .Document is initilialized

            if (wnd.popupBrowser.Document == null)
                wnd.popupBrowser.Navigate(new Uri("about:blank"));

            Guid SID_SWebBrowserApp = new Guid("0002DF05-0000-0000-C000-000000000046");
            IServiceProvider serviceProvider = (IServiceProvider)wnd.popupBrowser.Document;

            Guid serviceGuid = SID_SWebBrowserApp;

            Guid iid = typeof(SHDocVw.IWebBrowser2).GUID;

            SHDocVw.IWebBrowser2 myWebBrowser2 = (SHDocVw.IWebBrowser2)serviceProvider.QueryService(ref serviceGuid, ref iid);


            ppDisp = myWebBrowser2.Application;

        }

        public void showPopup()
        {
            if (wnd != null)
            {
                wnd.Top = this.Top + 30;
                wnd.Left = this.Left + 155;
                wnd.Show();
            }
        }

    }
}
